import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC

# 启动 Chrome 浏览器
driver = webdriver.Chrome()

# 打开网址
url = 'https://www.resonance-columba.com/route'
driver.get(url)

# 等待按钮可点击
button_luxian = '//*[@id="root"]/body/header/div/div/div[2]/a[2]/button'
button = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, button_luxian))
)
# 点击按钮
button.click()

# 点击来回循环和单位疲劳利润复选框
checkbox_laihui = '//*[@id="root"]/body/main/div/div/div[2]/div[2]/div[2]/label[1]/p'
checkbox_danweipilaolirun = '//*[@id="root"]/body/main/div/div/div[2]/div[2]/div[2]/label[2]/p'

checkbox = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, checkbox_laihui))
)
checkbox.click()

checkbox = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, checkbox_danweipilaolirun))
)
checkbox.click()

# 输入单位疲劳利润的新值
input_element = driver.find_element(By.XPATH,'//input[@id=":R734lfbrda:"]')
input_element.clear()  # 清空输入框
input_element.send_keys(Keys.BACKSPACE)
new_value = "0"  # 新值
input_element.send_keys(new_value)  # 发送新值
ActionChains(driver).move_by_offset(100, 100).click().perform()

# 确定路径上的起点和终点，以及最大单位疲劳利润
begin = 0
finish = 0
Max_elements_danweipilaolirun = 0
time.sleep(5)
for i in range(1, 10):
    for j in range(3, 11):
        if (i == 1 and j == 3) or (i == j - 1):
            continue
        else:
            elements_danweipilaolirun = driver.find_element(By.XPATH,
                                                            f'//*[@id="root"]/body/main/div/div/div[2]/div[3]/div[2]/table/tbody/tr[{i}]/td[{j}]/button/span[2]').text
            if int(elements_danweipilaolirun) > Max_elements_danweipilaolirun:
                begin = i
                finish = j - 1
            Max_elements_danweipilaolirun = int(elements_danweipilaolirun) if int(elements_danweipilaolirun) >=  Max_elements_danweipilaolirun else Max_elements_danweipilaolirun

# 加载城市数据
with open('city.json', 'r', encoding='utf-8') as file:
    city = json.load(file)

# 输出最大单位疲劳利润及路径信息
print("最大单位疲劳利润为:", Max_elements_danweipilaolirun)
print(f"路径为：{city[f'{begin}']} → {city[f'{finish}']} → {city[f'{begin}']} 循环行驶")

# 构建路径字典
path = {
    f"{begin}": city[f'{begin}'],
    f"{finish}": city[f'{finish}']
}

# 点击确定按钮
button_e = f'//*[@id="root"]/body/main/div/div/div[2]/div[3]/div[2]/table/tbody/tr[{begin}]/td[{finish+1}]/button/span[2]'
button = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, button_e))
)
button.click()

# 获取数据
elements_itemdata1 = driver.find_element(By.XPATH,f'//*[@id="root"]/body/div[2]/div[3]/div/div[1]/div[1]/p[4]').text
elements_itemdata2 = driver.find_element(By.XPATH,f'//*[@id="root"]/body/div[2]/div[3]/div/div[1]/div[2]/p[4]').text
elements_itemdata3 = driver.find_element(By.XPATH,f'//*[@id="root"]/body/div[2]/div[3]/div/div[1]/div[3]/p[3]').text

# 输出数据
print(city[f'{begin}'], elements_itemdata2)
print(city[f'{finish}'], elements_itemdata1)
print(elements_itemdata3)

# 将路径写入 JSON 文件
with open('path.json', 'w',encoding='GBK') as f:
    json.dump(path, f, ensure_ascii=False, indent=4)
